﻿using MediatR;
using RBAC_Uthorization.Domain;
using RBAC_Uthorization.Infrstructre.Interface;
using RBAC_Uthorization.Read.API.Application.Command.MenuCommand;
using RBAC_Uthorization.Read.API.DTO;
using RBAC_Uthorization.Read.API.DTO.Response.Menu;

namespace RBAC_Uthorization.Read.API.Application.CommandHandlers.MenuHandler
{
    public class MenuSelectShowCommandHandlers : IRequestHandler<MenuSelectShowCommand, ApiResult<List<MenuSelectShow>>>
    {
        private readonly IMenuRepository _menuRepository;
        private readonly ILogger<MenuSelectShowCommandHandlers> _logger;

        public MenuSelectShowCommandHandlers(ILogger<MenuSelectShowCommandHandlers> logger, IMenuRepository menuRepository)
        {
            _logger = logger;
            _menuRepository = menuRepository;
        }

        public async Task<ApiResult<List<MenuSelectShow>>> Handle(MenuSelectShowCommand request, CancellationToken cancellationToken)
        {
            try
            {

                var menu = await _menuRepository.GetAllAsunc(x=> x.sys_id == request.sys_id&&x.del_flag==0);
                if (menu == null)
                {
                    return new ApiResult<List<MenuSelectShow>> { Code = ResultCode.NoContent, Message = "菜单不存在" };
                }
                var menuSelectShow = menu.Select(x => new MenuSelectShow
                {
                    id = x.id,
                    menu_name = x.menu_name,
                }).ToList();
                return new ApiResult<List<MenuSelectShow>> { Data = menuSelectShow };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取菜单信息失败");
                return new ApiResult<List<MenuSelectShow>> { Code = ResultCode.Fail, Message = "获取菜单信息失败" };
            }
        }
    }
}
